<?php

namespace common\core\base;

use common\helpers\BaseHelper;
use common\helpers\CodeHelper;
use yii\base\UserException;
use yii\data\Pagination;
use yii\db\Query;
use yii\helpers\ArrayHelper;

trait InvalidException
{

    /**
     * 参数无效异常
     *
     * @param int    $code
     * @param string $message
     *
     * @return mixed
     * @throws UserException
     */
    public function invalidParamException($message = '', $code = CodeHelper::SYS_PARAMS_ERROR)
    {
        if ($message == '') {
            $message = CodeHelper::getCodeText($code);
        }
        BaseHelper::invalidException($code, $message);
    }

    /**
     * 表单无效异常
     *
     * @param int    $code
     * @param string $message
     *
     * @return mixed
     * @throws UserException
     */
    public function invalidFormException($message = '', $code = CodeHelper::SYS_FORM_ERROR)
    {
        if ($message == '') {
            $message = CodeHelper::getCodeText($code);
        }
        BaseHelper::invalidException($code, $message);
    }

    /**
     * 请求无效异常
     *
     * @param int    $code
     * @param string $message
     *
     * @return mixed
     * @throws UserException
     */
    public function invalidRequestException($message = '', $code = CodeHelper::SYS_REQUEST_ERROR)
    {
        if ($message == '') {
            $message = CodeHelper::getCodeText($code);
        }
        BaseHelper::invalidException($code, $message);
    }

    /**
     * 判断字段 是否 存在并且不为空
     *
     * @param array  $params
     * @param string $filed
     * @param bool   $isValidateZero 是否验证
     *
     * @return bool
     */
    public function issetAndEmpty(array $params, $filed, $isValidateZero = false)
    {
        $status = false;
        if (isset($params[$filed])) {
            if ($isValidateZero && ($params[$filed] === '0' || $params[$filed] === 0)) {
                $status = true;
            }
            if (!empty($params[$filed])) {
                $status = true;
            }
        }
        return $status;
    }

    /**
     * 验证请求参数字段
     * 支持别名
     *
     * @param array $requests      请求参数
     * @param array $validateField 验证字段，支持别名  ['别名' => 字段， 0 => 字段]
     * @param string         /array $default       字段默认值
     *
     * @return array
     * @author lengbin(lengbin0@gmail.com)
     */
    public function validateRequestParams($requests, $validateField, $default = '')
    {
        $data = [];
        foreach ($validateField as $key => $field) {
            $param = isset($requests[$field]) ? $requests[$field] : null;
            if ($default !== '' && $param === null) {
                if (is_array($default)) {
                    $param = (isset($default[$field])) ? $default[$field] : null;
                } else {
                    $param = $default;
                }
            }
            if (is_int($key)) {
                $data[$field] = $param;
            } else {
                $data[$key] = $param;
            }
        }
        return $data;
    }

    /**
     * 是否接口
     *
     * @return bool
     */
    public function isApi()
    {
        if (!\Yii::$app->user->isGuest) {
            $user = \Yii::$app->user->identity;
            $api = $user::$isApi === null ? false : $user::$isApi;
        } else {
            $api = ArrayHelper::getValue(\Yii::$app->params, 'isApi', false);
        }

        return $api;
    }

    /**
     * 分页
     *
     * @param Query/array $params
     * @param int $pageSize
     *
     * @return array
     *
     * @auth ice.leng(lengbin@geridge.com)
     * @issue
     */
    public function page($params, $pageSize = 10)
    {
        if ($params instanceof Query) {
            $count = $params->count();
        } else {
            $count = count($params);
        }
        $pageSize = ArrayHelper::getValue(\Yii::$app->request->getQueryParams(), 'per-page', $pageSize);
        $pages = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        if ($params instanceof Query) {
            $models = $params->offset($pages->offset)->limit($pages->limit)->all();
        } else {
            /* @var $params array */
            $models = array_slice($params, $pages->offset, $pages->limit);
        }
        return [
            'models' => $models,
            'pages'  => $pages,
        ];
    }


    public function mongodbPage($params, $pageSize = 10)
    {

        if ($params instanceof \yii\mongodb\Query) {
            $count = $params->count();
        } else {
            $count = count((array)$params);
        }
        $pageSize = ArrayHelper::getValue(\Yii::$app->request->getQueryParams(), 'per-page', $pageSize);
        $pages = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        if ($params instanceof \yii\mongodb\Query) {
            $models = $params->offset($pages->offset)->limit($pages->limit)->all();
        } else {
            /* @var $params array */
            $models = array_slice($params, $pages->offset, $pages->limit);
        }
        return [
            'models' => $models,
            'pages'  => $pages,
        ];
    }
}